<?php

/**
 * 默认关注管理 -- 推荐提交程序
 * 
 * @copyright caijian
 * @since 2012-03-20
 */




include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/admin.inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/foodv2/inc.php");

$g_cgival = array();
$g_pro = array();
$g_show = array();


function check_cgi_pro()
{
	global $g_cgival, $g_pro, $g_show;
	
	
	// id
	cgi::both($g_cgival["uid"], "uid", 0);
	$g_cgival["uid"] = intval($g_cgival["uid"]);

	
	// op
	cgi::both($g_cgival["op"], "op", "");
	
	// site_id
	cgi::both($g_cgival["site_id"], "site_id", 0);
	$g_cgival["site_id"] = intval($g_cgival["site_id"]);
}

function get_data()
{
	global $g_cgival, $g_pro, $g_show;
	
	$uid = (int)$g_cgival["uid"];	
	$op = $g_cgival["op"];

	if (! $op )
	{
		throw new Exception("参数错误！");
	}

	if ($uid <= 0)
	{
		throw new Exception("提交的参数有错误！");
	}
	
	
	// 拿到当前站点的site_id
	$site_id = admin::admin_get_user_site_id();
	$admin_id = admin::admin_get_login_uid();
	$admin_name = admin::admin_get_login_user_name();

//  	$db->debug = true;
	$admin_log = new admin_log();
	$db = new db_food_user_offical_user();
	$fuser = new mod_food_user();
	

	// 判断
	switch ($op) {

	case "add":
		$daren = $fuser->get_food_user_info($uid);

		if ($daren['error'] != '0') {
			throw new Exception("达人不存在:".$daren['errmsg'] . '('.$uid.')');
		}

		$ret = $db->admin_insert_food_user_offical_user($uid, $site_id, $daren['nickname'], $admin_id, $admin_name);
		$data = $db->admin_get_food_user_offical_user_by_site_id($site_id);
		if (count($data['data']) > 6) {
			for ($i = 6;$i<count($data['data']); $i++) {
				$uid = $data['data'][$i]['uid'];
				$db->admin_update_food_user_offical_user_status($uid, $site_id, 0, 1, $admin_id, $admin_name);
			}
		}
		$admin_log->admin_insert_log_info($admin_id, $site_id, 8, "删除 默认关注uid={$uid}");
		break;
	
	case "del":
		if ($site_id > 0 && $site_id != $g_cgival['site_id']) {
			throw new Exception("站点权限错误");
		}

		$ret = $db->admin_update_food_user_offical_user_status($g_cgival['uid'], $g_cgival['site_id'], 0, 1, $admin_id, $admin_name);
		$admin_log->admin_insert_log_info($admin_id, $site_id, 8, "删除 默认关注uid={$uid}");
	}

	$g_show["ret"] = $ret;
}

try
{

	admin_check_login();
// 	admin_check_user_priv("foodv2/main_nav.php");
	
	check_cgi_pro();
	get_data();
	
	$g_show["error"] = 0;
	$g_show["errmsg"] = "成功";	

}
catch(Exception $e)
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
}

show_pro();
exit;


function show_pro()
{
	global $g_show;
	
	echo json_encode($g_show);
}

?>